<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/26
 * Time: 16:54
 */

namespace Home\Controller;
use Think\Controller;

class BaseController extends Controller {


    public function __construct()
    {
        session_start();
        parent::__construct();
    }
    /*
     * 发送图片地址请求七牛云接口获得图片的宽高
     * @param string $url 接口请求的地址
     * @param array $data  返回的宽高信息
     * return 返回宽高信息
     */
    public function imageinfo($url)
    {
        $result = $this->get($url.'?imageInfo');
        $res = json_decode($result,true);
        $data = array(
            'width'=>$res['width'],
            'height'=>$res['height']
        );
        return $data;
    }

    /*
     * curl 发送get请求
     * @param string $url  get请求的地址
     * return 返回get请求之后得到的数据
     */
    public function get($url)
    {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
       return $data;
    }

    /*
     * 将图片加水印模拟上传提交到七牛云
     * @param string $path 临时文件的路径
     * @param string $user_id 用户的id
     * @param string $type 上传的图片类型
     * @param string $img 将图片进行加水印
     */
    function upload_curl_pic($path,$user_id,$type)
    {
        $image = new \Think\Image();
// 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
        $logo = "./Public/images/upload/logo.png";
        $number = rand(1000,9999);
        $cutter = "./Public/images/upload/".$user_id.$number."thumb.jpg";
        $save = "./Public/images/upload/".$user_id.$number.".jpg";//设置水印图片路径
        $image->open($path);
        $image->thumb(1080, 1920,\Think\Image::IMAGE_THUMB_SCALE)->save($cutter);
        $image->open($cutter)->water($logo,\Think\Image::IMAGE_WATER_SOUTHEAST)->save($save);
        $url  = 'http://www.hzparty.com/index.php/base/qiniu';//服务器上的地址
//        $url  = 'http://127.0.0.1/svnwelove/index.php/base/qiniu';//本地地址

        $fields = array(
            "user_id"=>$user_id,
            "type"=>$type,
            "file"=>"@".$save
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
//        var_dump($result);
        return $result;
    }

    /*
     * 将图片上传到七牛并进行写入数据库
     * @param string $user_id 用户id
     * @param string $type 图片类型
     * @param string $url 带水印图片上传到七牛之后获取到的图片url
     */
    public function qiniu()
    {
        $user_id = I('post.user_id');
        $type = I('post.type');
        $model = D('images');
        $setting=C('UPLOAD_SITEIMG_QINIU');
        $Upload = new \Think\Upload($setting);
        $info = $Upload->upload($_FILES);
        $url = $info['file']['url'];
        $image = $this->imageinfo($url);
            $data = array(
                'user_id'=>$user_id,
                'pic_url'=>$url,
                'width'=>$image['width'],
                'height'=>$image['height'],
                'type'=>$type,
                'up_time'=>date('Y-m-d H:i:s',time()),
            );
         $model->data($data)->add();
    }

    /*
  * 将图片加水印模拟上传提交到七牛云
  * @param string $path 临时文件的路径
  * @param string $user_id 用户的id
  * @param string $img 将图片进行加水印
  */
    function upload_header_pic($path,$user_id)
    {
        $image = new \Think\Image();
// 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
        $logo = "./Public/images/upload/logo.png";
        $number = rand(1000,9999);
        $cutter = "./Public/images/upload/".$user_id.$number."thumb.jpg";
        $save = "./Public/images/upload/".$user_id.$number.".jpg";//设置水印图片路径
        $image->open($path);
        $image->thumb(1080, 1920,\Think\Image::IMAGE_THUMB_SCALE)->save($cutter);
        $image->open($cutter)->water($logo,\Think\Image::IMAGE_WATER_SOUTHEAST)->save($save);
        $url  = 'http://www.hzparty.com/index.php/base/header_qiniu';
//        $url  = 'http://127.0.0.1/svnwelove/index.php/base/header_qiniu';

        $fields = array(
            "user_id"=>$user_id,
            "file"=>"@".$save
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        curl_close($ch);
//        var_dump($result);
        return $result;
    }

    /*
  * 将用户头像上传到七牛云并进行写入数据库
  * @param string $user_id 用户id
  * @param string $url 带水印图片上传到七牛之后获取到的图片url
  */
    public function header_qiniu()
    {
        $model = D('userinfo');
        $user_id = I('post.user_id');
        $setting=C('UPLOAD_SITEIMG_QINIU');
        $Upload = new \Think\Upload($setting);
        $info = $Upload->upload($_FILES);
        $url = $info['file']['url'];
        $data = array(
            'header_pic'=>$url,
        );
        $condition['user_id'] = $user_id;
        $model->where($condition)->save($data);
    }

    /*
     * 判断是否是会员
     * @param int $id 传入的用户id
     * @param int $statu 判断会员状态，1为非会员，2为会员
     * @param int $count 非会员的心动次数
     */
    public function check_vip($id,$type)
    {
        $model = D('user');
        $condition['id'] = $id;
        $res = $model->where($condition)->field('statu')->select();
        $statu = $res[0]['statu'];
        //如果不是会员就判断其当日心动次数是否为3次
        if($statu==1)
        {
            if($type==1)
            {
                $count = $this->judge_heart_times($id);
            }elseif($type==2)
            {
                $count = $this->judge_chat_time($id);
            }
            if($count<3)
            {
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }

    /*
     * 判断心动次数
     * @param int $id 用户的id
     *@param int $count 心动次数
     * return 返回当日心动次数
     */
    public function judge_heart_times($id)
    {
        $model = D('heart');
       $sql = "select count(frombeat) as count from wl_heart where frombeat=$id and TO_DAYS(beatime)=TO_DAYS(now())";
        $res = $model->query($sql);
       $count = $res[0]['count'];
        return $count;
    }

    /*
     * 判断聊天次数
     * @param int $id 用户的id
     * @param int $count 当日聊天的次数
     * return 返回当日聊天的次数
     */
    public function judge_chat_time($id)
    {
//        $id = 1000;
        $model = D('chat');
        $sql = "select count(user_id) as count from wl_chat where user_id=$id and TO_DAYS(chat_time)=TO_DAYS(now())";
        $res = $model->query($sql);
        $count = $res[0]['count'];
        return $count;

    }

    /*
     * 根据唯一用户id创建邀请码
     * @param int $user_id 用户的唯一识别id
     * @param string $yard 生成的邀请码：id+随机数格式
     * return 返回邀请码
     */
    function createCode()
    {
        $num = rand(1000,9999);
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0,25)]
            .strtoupper(dechex(date('m')))
            .date('d').substr(time(),-5)
            .substr(microtime(),2,5)
            .sprintf('%02d',rand(0,99));
        for(
            $a = md5( $rand, true ),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 2;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 2 ] ) ) - $g & 0x1F ],
            $f++
        );
        $yard = $num.$d;
        return $yard;
    }

    //海报照片
    public function poster()
    {
        $model = M('images');
        $condition['type'] = 6;
        $data = $model->where($condition)->select();
        $url = $data[0]['pic_url'];
        $result['url'] = $url;
        $result['width'] = $data[0]['width'];
        $result['height'] = $data[0]['height'];
        $result['href'] = "http://www.hzparty.com/index.php/activity/show_html";
        echo json_encode($result);
    }



}